//
//  DrawView.m
//  CAShapeLayerDemo
//
//  Created by XFB on 2017/11/28.
//  Copyright © 2017年 ZhouCheng. All rights reserved.
//

#import "DrawView.h"
#import "UIColor+Extension.h"

#define  ConversionRadian(degrees)    (( M_PI* degrees)/ 180)
#define  percentageToRadian(percentage)      (( M_PI* percentage)/ 240)

@interface DrawView ()

@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (nonatomic, assign) NSInteger percentageStart;
@property (nonatomic, strong) UILabel *percentLabel;

@end

@implementation DrawView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        CAShapeLayer *circleLayer = [CAShapeLayer layer];
        circleLayer.frame = self.bounds;
        [self.layer addSublayer:circleLayer];
        
        circleLayer.fillColor = [UIColor clearColor].CGColor;
        circleLayer.strokeColor = [UIColor lightGrayColor].CGColor;
        circleLayer.opacity = 0.25;//背景圆环的背景透明度
        circleLayer.lineCap = kCALineCapRound;
        circleLayer.lineWidth = 10;
        
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width/2, frame.size.height/2) radius:80 startAngle:ConversionRadian(-210) endAngle:ConversionRadian(30) clockwise:YES];
        circleLayer.path = path.CGPath;
        
        self.progressLayer = [CAShapeLayer layer];
        self.progressLayer.frame = self.bounds;
        self.progressLayer.fillColor =  [[UIColor clearColor] CGColor];
        self.progressLayer.strokeColor = [UIColor.orangeColor CGColor];
        self.progressLayer.lineCap = kCALineCapRound;
        self.progressLayer.lineWidth = 10;
        self.progressLayer.path = [path CGPath];
        self.progressLayer.strokeEnd = 0;
        
        CALayer *layerBG = [CALayer layer];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width/2, self.bounds.size.height);
        [gradientLayer setColors:[NSArray arrayWithObjects:(id)[UIColor yellowColor].CGColor,(id)[UIColorFromRGB(0xff0000) CGColor], nil]];
        [gradientLayer setStartPoint:CGPointMake(0.5, 0.2)];
        [gradientLayer setEndPoint:CGPointMake(0.5, 0.5)];
        [layerBG addSublayer:gradientLayer];
        
        CAGradientLayer *gradientLayer1 =  [CAGradientLayer layer];
        gradientLayer1.frame = CGRectMake(self.frame.size.width/2, 0, self.frame.size.width/2, self.frame.size.height);
        [gradientLayer1 setColors:[NSArray arrayWithObjects:(id)[UIColor yellowColor].CGColor,(id)[UIColor.blueColor CGColor], nil]];
        [gradientLayer1 setStartPoint:CGPointMake(0.5, 0.2)];
        [gradientLayer1 setEndPoint:CGPointMake(0.5, 0.5)];
        
        [layerBG addSublayer:gradientLayer1];
        [layerBG setMask:self.progressLayer]; //用progressLayer来截取渐变层
        [self.layer addSublayer:layerBG];
        
        self.percentLabel = [[UILabel alloc] initWithFrame:self.bounds];
        self.percentLabel.text = @"";
        self.percentLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:30];
        self.percentLabel.textColor = [UIColor purpleColor];
        [self addSubview:self.percentLabel];
        self.percentLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    return self;
}

-(void)setPercent:(NSInteger)percent animated:(BOOL)animated
{
    [CATransaction begin];
    [CATransaction setDisableActions:!animated];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1];
    self.progressLayer.strokeEnd = percent/100.0;
    [CATransaction commit];
    
    
    self.percentLabel.text = [NSString stringWithFormat:@"%ld%%", (long)percent];
}


@end
